package com.sogoodsoft.web.account;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import com.sogoodsoft.entity.account.Authority;
import com.sogoodsoft.service.account.AuthorityService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.web.CrudActionSupport;
import com.sogoodsoft.web.WebConstants;

@Namespace("/account")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "authority.action", type = "redirect") })
public class AuthorityAction extends CrudActionSupport < Authority > {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private AuthorityService authoritySer;

    private Long id;

    private Authority entity;

    private List < Authority > list;

    private Page < Authority > page = new Page < Authority >(WebConstants.PAGE_SIZE);

    @Override
    public String delete() throws Exception {
        entity = authoritySer.get(id);
        authoritySer.delete(entity);
        Struts2Utils.renderJson(DwzMsg.success());
        return null;
    }

    @Override
    public String input() throws Exception {
        return INPUT;
    }

    @Override
    public String list() throws Exception {
        List < PropertyFilter > filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
        // 设置默认排序方式
        if (!page.isOrderBySetted()) {
            page.setOrderBy("cate");
            page.setOrder(Page.ASC);
        }

        logger.debug("查询权限列表");
        page = authoritySer.findPage(page, filters);
        return SUCCESS;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = authoritySer.get(id);
        } else {
            entity = new Authority();
        }
    }

    @Override
    public String save() throws Exception {
        authoritySer.save(entity);
        Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
        return null;
    }

    @Override
    public Authority getModel() {
        return entity;
    }

    public AuthorityService getAuthoritySer() {
        return authoritySer;
    }

    @Autowired
    public void setAuthoritySer(AuthorityService authoritySer) {
        this.authoritySer = authoritySer;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List < Authority > getList() {
        return list;
    }

    public void setList(List < Authority > list) {
        this.list = list;
    }

    public Page < Authority > getPage() {
        return page;
    }

    public void setPage(Page < Authority > page) {
        this.page = page;
    }

    public Authority getEntity() {
        return entity;
    }

    public void setEntity(Authority entity) {
        this.entity = entity;
    }

}
